﻿using System;
using BattleBotsMattAndSunny.Model;
using Robocode;
using Robocode.Util;

namespace BattleBotsMattAndSunny.Utilities
{
    public static class CommonActions
    {
        public static TurnCorrection GetCorrection(double scanHeading, double lockingHeading)
        {
            var difference = scanHeading - lockingHeading;
            var correction = new TurnCorrection
            {
                Direction = (difference > 180 || difference < 0) ? TurnDirection.Right : TurnDirection.Left,
                Degrees = Math.Abs(difference)
            };
            if (Math.Abs(difference) < 2) correction.Direction = TurnDirection.None;
            return correction;
        }

        public static double AngleToPoint(double x, double y, AdvancedRobot robot)
        {
            // can rotate the coordinate system to avoid the addition of pi/2 if you like
            double b = Math.PI / 2 - Math.Atan2(y - robot.Y, x - robot.Y);
            return Utils.NormalRelativeAngle(b - robot.HeadingRadians);
        }

        public static double AngleToCenter(AdvancedRobot robot)
        {
            return AngleToPoint(robot.BattleFieldWidth / 2, robot.BattleFieldHeight / 2, robot);
        }
    }
}